package be.teletask.onvif.parsers;

import be.teletask.onvif.models.GetAudioDecoderConfigurationOptions;
import be.teletask.onvif.models.GetAudioDecoderConfigurationOptionsItem;
import be.teletask.onvif.models.OnvifProfile;
import be.teletask.onvif.responses.OnvifResponse;
import be.teletask.onvif.util.IsXmlUtil;
import be.teletask.onvif.util.XmlToJsonUtils;
import com.alibaba.fastjson.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.StringReader;

/**
 * Created by Tomas Verhelst on 04/09/2018.
 * Copyright (c) 2018 TELETASK BVBA. All rights reserved.
 */
public class GetAudioDecoderConfigurationOptionsParser extends OnvifParser<GetAudioDecoderConfigurationOptions> {

    //Constants
    public static final String TAG = GetAudioDecoderConfigurationOptionsParser.class.getSimpleName();
    private static final String KEY_OPTIONNS = "GetAudioDecoderConfigurationOptionsResponse";
    private static final String KEY_ONE = "AACDecOptions";
    private static final String KEY_TWO = "G711DecOptions";
    private static final String KEY_THREE = "G726DecOptions";
    private static final String ATTR_BITRATE = "Bitrate";
    private static final String ATTR_SAMPLE = "SampleRateRange";

    @Override
    public GetAudioDecoderConfigurationOptions parse(OnvifResponse response) {
        GetAudioDecoderConfigurationOptions audioDecoder = new GetAudioDecoderConfigurationOptions();
        String xml = response.getXml();
        if (xml.contains("<trt:GetAudioDecoderConfigurationOptionsResponse>")) {
            if (IsXmlUtil.isXmlDocument(xml)) {
                JSONObject bodyObjet = XmlToJsonUtils.xmlToJson(response.getXml());
                JSONObject optionsObject = bodyObjet.getJSONObject("GetAudioDecoderConfigurationOptionsResponse").getJSONObject("Options");

                String aACDecOptions = optionsObject.getJSONObject("AACDecOptions").getJSONObject("SampleRateRange").getString("Items");
                String aACDecOptionsBitrate = optionsObject.getJSONObject("AACDecOptions").getJSONObject("Bitrate").getString("Items");
                GetAudioDecoderConfigurationOptionsItem optionsItemAAC = new GetAudioDecoderConfigurationOptionsItem();
                optionsItemAAC.setSampleRateRange(aACDecOptions);
                optionsItemAAC.setBitrate(aACDecOptionsBitrate);
                audioDecoder.setAacDecOptions(optionsItemAAC);

                String g711DecOptions = optionsObject.getJSONObject("G711DecOptions").getJSONObject("SampleRateRange").getString("Items");
                String g711DecOptionsBitrate = optionsObject.getJSONObject("G711DecOptions").getJSONObject("Bitrate").getString("Items");
                GetAudioDecoderConfigurationOptionsItem optionsItemG711Dec = new GetAudioDecoderConfigurationOptionsItem();
                optionsItemG711Dec.setSampleRateRange(g711DecOptions);
                optionsItemG711Dec.setBitrate(g711DecOptionsBitrate);
                audioDecoder.setG711DecOptions(optionsItemG711Dec);

                String g726DecOptions = optionsObject.getJSONObject("G726DecOptions").getJSONObject("SampleRateRange").getString("Items");
                String g726DecOptionsBitrate = optionsObject.getJSONObject("G726DecOptions").getJSONObject("Bitrate").getString("Items");
                GetAudioDecoderConfigurationOptionsItem optionsItemG726Dec = new GetAudioDecoderConfigurationOptionsItem();
                optionsItemG726Dec.setSampleRateRange(g726DecOptions);
                optionsItemG726Dec.setBitrate(g726DecOptionsBitrate);
                audioDecoder.setG726DecOptions(optionsItemG726Dec);
            }
        }

        return audioDecoder;
    }

}
